package com.example.lp.lpuicore.views.touch.screencheck;

import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.example.lp.lpuicore.R;

import java.util.ArrayList;
import java.util.List;

public class ScreenCheckActivity extends AppCompatActivity implements CheckAdapter.ChcekListtner {
    private RecyclerView screenCheckRv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_check);
        initViews();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initViews() {
        screenCheckRv = findViewById(R.id.rv_check);
        screenCheckRv.setLayoutManager(new GridLayoutManager(this, 5));
        List<Boolean> list = new ArrayList<>();
        //添加元素
        for (int i = 0; i < 25; i++) {
            list.add(false);
        }
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int height = dm.heightPixels;
        int width = dm.widthPixels;
        final CheckAdapter checkAdapter = new CheckAdapter(this, height, 5, list, this);
        screenCheckRv.setAdapter(checkAdapter);
        //监听滑动
        screenCheckRv.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                int moveX = (int) event.getX();
                int moveY = (int) event.getY();
                //寻找当前触摸所在view
                View childViewUnder = screenCheckRv.findChildViewUnder(moveX, moveY);
                //如果存在view
                if (childViewUnder != null) {
                    if (childViewUnder instanceof RelativeLayout) {
                        checkAdapter.moveCheck(childViewUnder);
                    }
                }
                return true;
            }
        });
    }

    @Override
    public void checkResult(Boolean allCheck) {
        if(allCheck){
            Log.i("ScreenCheckActivity", "全选！ ");
        }

    }
}
